package controllers

import java.io.File

import dao.ModeDao
import implicits.Implicits._
import javax.inject.Inject
import play.api.libs.json.Json
import play.api.mvc.{AbstractController, ControllerComponents}
import tool.{FormTool, Tool}

import scala.concurrent.ExecutionContext.Implicits.global


/**
 * Created by Administrator on 2019/8/8
 */
class DatController @Inject()(cc: ControllerComponents, formTool: FormTool)(implicit val modeDao: ModeDao) extends AbstractController(cc) {

  def datExportBefore = Action { implicit request =>
    Ok(views.html.user.datExport())
  }

  def xml2Dat = Action(parse.multipartFormData) { implicit request =>
    val data = FormTool.kitNameForm.bindFromRequest().get
    val tmpDir = Tool.createTempDirectory("tmpDir")
    val xmlFile = Tool.fileMoveDir("xmlFile", tmpDir)
    val datFile = new File(tmpDir, "out.dat")
    Tool.xml2Dat(xmlFile, datFile, data.kitName)
    Ok.sendFile(datFile, onClose = () => {
      Tool.deleteDirectory(tmpDir)
    })
  }


}
